package it.unibo.iss.domotica.consumptionControlling;

import java.util.Observer;

public class ConsumptionControllerFacade implements IConsumptionController {

	ConsumptionCalc calculator;
	TotalConsumption consumption;
	
	public ConsumptionControllerFacade(double threshold) {
		this.consumption = new TotalConsumption(threshold);
		calculator = new ConsumptionCalc(this.consumption);
	}
	
	public ConsumptionControllerFacade(ConsumptionCalc calculator, TotalConsumption consumo) {
		this.calculator = calculator;
		this.consumption = consumo;
	}
	
	@Override
	public double getCurrentConsumption() {
		return consumption.getCurrentConsumption();
	}

	@Override
	public void updateConsumption() {
		calculator.forceCalc();
	}

//	@Override
//	public void attach(TotalConsumptionObserver o) {
//		consumption.attach(o);		
//	}
	
	@Override
	public void attach(Observer o) {
		consumption.addObserver(o);
	}

//	@Override
//	public void detach(TotalConsumptionObserver o) {
//		consumption.detach(o);
//	}
	
	@Override
	public void detach(Observer o) {
		consumption.deleteObserver(o);
	}

	public void start() {
		calculator.start();
	}
	
	public void stop() {
		calculator.setStopThread(true);
	}

	@Override
	public boolean canIPowerOnED(double consumption) {
		return (calculator.approximateConsumption(consumption) <= this.consumption.getThreshold());
	}

	@Override
	public boolean thresholdExceeded() {
		return consumption.isAlert();
	}
}
